package com.iwomy.secureplat.platform.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iwomy.secureplat.common.core.util.R;
import com.iwomy.secureplat.common.log.annotation.SysLog;
import com.iwomy.secureplat.platform.dto.AlarmEventTypeDTO;
import com.iwomy.secureplat.platform.entity.AlarmDetectionTypeEntity;
import com.iwomy.secureplat.platform.entity.AlarmEventTypeEntity;
import com.iwomy.secureplat.platform.service.AlarmDetectionTypeService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.iwomy.secureplat.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 报警事件类型
 *
 * @author pig
 * @date 2023-12-22 10:10:01
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/alarmDetectionType")
@Tag(description = "alarmDetectionType", name = "报警事件类型管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class AlarmDetectionTypeController {

	private final AlarmDetectionTypeService alarmDetectionTypeService;

	/**
	 * 分页查询
	 *
	 * @param page               分页对象
	 * @param alarmDetectionType 报警事件类型
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('platform_alarmDetectionType_view')")
	public R getAlarmDetectionTypePage(@ParameterObject Page page, @ParameterObject AlarmDetectionTypeEntity alarmDetectionType) {
		LambdaQueryWrapper<AlarmDetectionTypeEntity> wrapper = Wrappers.lambdaQuery(alarmDetectionType);
		return R.ok(alarmDetectionTypeService.page(page, wrapper));
	}

	/**
	 * 列表查询
	 *
	 * @param alarmDetectionType 报警事件类型
	 * @return
	 */
	@Operation(summary = "列表查询", description = "列表查询")
	@GetMapping("/list")
	@PreAuthorize("@pms.hasPermission('platform_alarmDetectionType_view')")
	public R getAlarmDetectionTypeList(@ParameterObject AlarmDetectionTypeEntity alarmDetectionType) {
		LambdaQueryWrapper<AlarmDetectionTypeEntity> wrapper = Wrappers.lambdaQuery(alarmDetectionType);
		return R.ok(alarmDetectionTypeService.list(wrapper));
	}


	/**
	 * 通过id查询报警事件类型
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('platform_alarmDetectionType_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(alarmDetectionTypeService.getById(id));
	}

	/**
	 * 新增报警事件类型
	 *
	 * @param alarmDetectionType 报警事件类型
	 * @return R
	 */
	@Operation(summary = "新增报警事件类型", description = "新增报警事件类型")
	@SysLog("新增报警事件类型")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('platform_alarmDetectionType_add')")
	public R save(@RequestBody AlarmDetectionTypeEntity alarmDetectionType) {
		return R.ok(alarmDetectionTypeService.save(alarmDetectionType));
	}

	/**
	 * 修改报警事件类型
	 *
	 * @param alarmDetectionType 报警事件类型
	 * @return R
	 */
	@Operation(summary = "修改报警事件类型", description = "修改报警事件类型")
	@SysLog("修改报警事件类型")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('platform_alarmDetectionType_edit')")
	public R updateById(@RequestBody AlarmDetectionTypeEntity alarmDetectionType) {
		return R.ok(alarmDetectionTypeService.updateById(alarmDetectionType));
	}

	/**
	 * 通过id删除报警事件类型
	 *
	 * @param ids id列表
	 * @return R
	 */
	@Operation(summary = "通过id删除报警事件类型", description = "通过id删除报警事件类型")
	@SysLog("通过id删除报警事件类型")
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('platform_alarmDetectionType_del')")
	public R removeById(@RequestBody Long[] ids) {
		return R.ok(alarmDetectionTypeService.removeBatchByIds(CollUtil.toList(ids)));
	}


	/**
	 * 导出excel 表格
	 *
	 * @param alarmDetectionType 查询条件
	 * @param ids                导出指定ID
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('platform_alarmDetectionType_export')")
	public List<AlarmDetectionTypeEntity> export(AlarmDetectionTypeEntity alarmDetectionType, Long[] ids) {
		return alarmDetectionTypeService.list(Wrappers.lambdaQuery(alarmDetectionType).in(ArrayUtil.isNotEmpty(ids), AlarmDetectionTypeEntity::getId, ids));
	}

	/**
	 * 启用禁用
	 *
	 * @param alarmDetectionTypeEntity
	 * @return R
	 */
	@Operation(summary = "启用禁用检测类型", description = "启用禁用检测类型")
	@SysLog(expression = "'启用禁用检测类型'.concat(#alarmDetectionTypeEntity.remarks)")
	@PostMapping("/enableOrDisable")
	public R enableOrDisable(@RequestBody AlarmDetectionTypeEntity alarmDetectionTypeEntity) {
		return R.ok(alarmDetectionTypeService.updateById(alarmDetectionTypeEntity));
	}
}